#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#include <conio.h>
#define HEIGHT 29
#define WIDTH 110 

const int dir[4][2] = {         //数组第一个坐标为y坐标
    {-1,0},         //向上
    {0,1},          //向右
    {1,0},          //向下
    {0,-1}          //向左
};

typedef enum BlockType {
    EMPTY = 0,
    FOOD = 1,
    BOUND = 2,
}BlockType;

typedef enum Direction {
    UP = 0,
    RIGHT = 1,
    DOWN = 2,
    LEFT = 3,
}Direction;

typedef struct Map
{
    BlockType data[HEIGHT][WIDTH];
    bool hasFood;
}Map;

typedef struct Pos {
    int x;
    int y;
}Pos;

typedef struct Snake {
    Pos snake[HEIGHT * WIDTH];
    int snakeLength;
    Direction snakeDirection;
    int lastMoveTime;
    int moveFrequency;
}Snake;
/*系统函数 */
void hideCursor();
void hideCursor() {
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO curInfo = { 1,FALSE };
    SetConsoleCursorInfo(hOutput, &curInfo);
}
void drawUnit(Pos p,char unit[]);
void drawUnitXY(int x,int y,char unit[]);
void drawIntXY(int x,int y,int unit);
void drawXDU();
void drawUnit(Pos p,char unit[]){
    COORD coord;
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    coord.X = p.x + 1;      //绘制地图时有一圈边框，所以要加上偏移量1
    coord.Y = p.y + 1;
    SetConsoleCursorPosition(hOutput,coord);
    printf("%s",unit);
}

void drawUnitXY(int x,int y,char unit[]){
    COORD coord;
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    coord.X = x + 1;      //绘制地图时有一圈边框，所以要加上偏移量1
    coord.Y = y + 1;
    SetConsoleCursorPosition(hOutput,coord);
    printf("%s",unit);
}

void drawIntXY(int x,int y,int unit){
    COORD coord;
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    coord.X = x + 1;      //绘制地图时有一圈边框，所以要加上偏移量1
    coord.Y = y + 1;
    SetConsoleCursorPosition(hOutput,coord);
    printf("%d",unit);
}
void drawXDU(){
    drawUnitXY(4,1,"█");
    drawUnitXY(8,1,"█");
    drawUnitXY(5,2,"█");
    drawUnitXY(7,2,"█");
    drawUnitXY(6,3,"█");
    drawUnitXY(5,4,"█");
    drawUnitXY(7,4,"█");
    drawUnitXY(4,5,"█");
    drawUnitXY(8,5,"█");
    drawUnitXY(10,1,"█");
    drawUnitXY(11,1,"█");
    drawUnitXY(12,1,"█");
    drawUnitXY(10,2,"█");
    drawUnitXY(13,2,"█");
    drawUnitXY(10,3,"█");
    drawUnitXY(14,3,"█");
    drawUnitXY(10,4,"█");
    drawUnitXY(13,4,"█");
    drawUnitXY(10,5,"█");
    drawUnitXY(11,5,"█");
    drawUnitXY(12,5,"█");
    drawUnitXY(16,1,"█");
    drawUnitXY(20,1,"█");
    drawUnitXY(16,2,"█");
    drawUnitXY(20,2,"█");
    drawUnitXY(16,3,"█");
    drawUnitXY(20,3,"█");
    drawUnitXY(16,4,"█");
    drawUnitXY(20,4,"█");
    drawUnitXY(17,5,"█");
    drawUnitXY(18,5,"█");
    drawUnitXY(19,5,"█");
    drawUnitXY(4,7,"MADE BY:");
    drawUnitXY(4,8,"ZIHAN WANG");
    drawUnitXY(4,9,"JUNTONG ZHAO");
}
/*地图及障碍物绘制函数*/
void initMap(Map* map);
void initBound(Map* map);
void drawMap(Map* map);
void initMap(Map* map){
    int x,y;
    for(y = 0;y < HEIGHT; y++){
        for(x = 0;x < WIDTH; x++){
            map->data[y][x] = EMPTY;
        }
    }
    map->hasFood = false;
}

void initBound(Map* map){
    int x,y;
    for(y = 20;y < 21; y++){
        for(x = 50;x < 56; x++){
            map->data[y][x] = BOUND;
        }
    }
    for(y = 12;y < 13; y++){
        for(x = 43;x < 47; x++){
            map->data[y][x] = BOUND;
        }
    }
    for(x = 43;x < 44;x++){
        for(y = 12;y < 17;y++){
            map->data[y][x] = BOUND;
        }
    }
    for(y = 5;y < 10; y++){
        for(x = 11;x < 12; x++){
            map->data[y][x] = BOUND;
        }
    }
}

void drawMap(Map* map){
    int i,j;
    system("cls");
    printf("╭");
    for(i = 0;i<WIDTH;i++){
        printf("─");
    }
    printf("╮\n");
    for(i = 0;i < HEIGHT;i++){
        printf("│"); 
        for(j = 0;j < WIDTH;j++){
            if(map->data[i][j] == EMPTY) printf(" ");
            else if(map->data[i][j] == BOUND) printf("█");
        }
        printf("│\n");
    }
    printf("╰");
    for(i = 0;i < WIDTH;i++){
        printf("─");
    }
    printf("╯");
}
